package component

import (
	"bgs/cache/redisImp"
	n "bgs/component/notifier"
	"bgs/conf"
	"bgs/db"
	"go.uber.org/zap"
)

// ComponentManger 组件管理器
type ComponentManger struct {
	Notifier *n.Notifier
}

var (
	_one *ComponentManger
)

//NewComponentManger 新建组件管理器
func InitComponentManger(
	slog *zap.SugaredLogger,
	pgdao *db.PgDao,
	vars *conf.VarsSection,
	adp *cache.Adapter,
	notifierConf conf.NotifierSection,
	wechatConfPtr *conf.WechatSection,
) (cm *ComponentManger) {
	slog = slog.With("entry", "component")
	cm = _one
	if cm == nil {
		notifier, err := n.NewNotifier(notifierConf, wechatConfPtr, slog, pgdao, vars, adp)
		if err != nil {
			slog.Fatalf("failed to serve: %v", err)
		}
		cm = &ComponentManger{
			Notifier: notifier,
		}
		if notifier.Ready {
			slog.Info("notifier ready")
		}
		slog.Info("all components started")
		_one = cm
	}

	return
}
